package com.hwy.widget.xx_ui

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hwy.widget.R

class KeUIActivity : AppCompatActivity() {

    private val dataItems = arrayOf(
        arrayOf("FlowLayout", FlowLayoutActivity::class.java)
    )

    private var recyclerView: RecyclerView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_ke_ui)

        initRecycler()
    }

    private fun initRecycler() {
        recyclerView = findViewById<RecyclerView>(R.id.recycler_view)?.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = object : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
                override fun onCreateViewHolder(
                    parent: ViewGroup,
                    viewType: Int
                ): RecyclerView.ViewHolder {
                    return ItemViewHolder(
                        LayoutInflater.from(context)
                            .inflate(R.layout.layout_main_adapter, parent, false)
                    )
                }

                override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
                    (holder as? ItemViewHolder)?.bindView(position)
                }

                override fun getItemCount(): Int {
                    return dataItems.size
                }

            }
        }

    }

    private inner class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bindView(position: Int) {
            itemView.findViewById<TextView>(R.id.tv_item)?.run {
                text = dataItems[position][0] as String
                setOnClickListener {
                    startActivity(Intent(this@KeUIActivity, dataItems[position][1] as Class<*>))
                }
            }
        }
    }
}